package cn.ll.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.web.servlet.ModelAndView;

import cn.ll.po.Customer;
import cn.ll.po.Department;
import cn.ll.po.Permissions;
import cn.ll.po.User;
import cn.ll.service.CustomerService;
import cn.ll.service.DepartmentService;
import cn.ll.service.UserService;
import cn.ll.util.SHA1;
import cn.ll.util.StringUtil;
import cn.ll.vo.LoginUser;

public class UserAction extends Action {

	UserService userService = null;

	DepartmentService departmentService = null;

	/**
	 * 用户登录代码
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = null;
		String uname = request.getParameter("uname");
		String password = request.getParameter("password");
		if (uname == null || uname.length() == 0) {
			String error = "请输入用户名";
			request.setAttribute("errmsg", error);
			mv = new ModelAndView("/user/login.jsp");
			return mv;
		}
		if (password == null || password.length() == 0) {
			String error = "请输入密码";
			request.setAttribute("errmsg", error);
			mv = new ModelAndView("/user/login.jsp");
			return mv;
		}
		User user = userService.login(uname, password);
		if (user == null) {
			String error = "登录失败，请输入正确的用户名和密码";
			request.setAttribute("errmsg", error);
			mv = new ModelAndView("/user/login.jsp");
			return mv;
		}
		Map permissions = userService.usrPermissions(user.getId());
		LoginUser loginUser = new LoginUser();
		loginUser.setUser(user);
		loginUser.setPermissions(permissions);
		List<Permissions> plist = userService.usrPermissionsList(user.getId());
		Department department = departmentService.findById(new Integer(user.getDepartmentid()));
		request.getSession().setAttribute("CURRENT_PERMISSIONS", plist);
		request.getSession().setAttribute("CURRENT_USER", loginUser);
		request.getSession().setAttribute("CURRENT_DEPARTMENT", department);
		mv = new ModelAndView("/index.jsp");
		return mv;
	}

	/**
	 * 用户登出
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = null;

		request.getSession().setAttribute("CURRENT_USER", null);
		request.getSession().setAttribute("CURRENT_PERMISSIONS", null);
		mv = new ModelAndView("/user/login.jsp");
		return mv;
	}

	/**
	 * 获得当前的
	 * @param request
	 * @param response
	 */
	public void getCurUsr(HttpServletRequest request, HttpServletResponse response) {
		LoginUser user = (LoginUser) request.getSession().getAttribute("CURRENT_USER");
		Map rs = new HashMap();
		List ls = new ArrayList();
		ls.add(user);
		rs.put("items", ls);
		rtn(rs, request, response);
	}

	/**
	 * 修改密码
	 * 
	 * @param request
	 * @param response
	 */
	public void modifyPassword(HttpServletRequest request, HttpServletResponse response) {
		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("CURRENT_USER");
		Map rs = null;
		if (loginUser != null) {
			String oldpassword = request.getParameter("oldpassword");
			String newpassword = request.getParameter("newpassword");
			User newuser = userService.read(loginUser.getUser().getId());
			if (newuser.getPassword().equalsIgnoreCase(StringUtil.sha1(oldpassword)) && newpassword != null
					&& newpassword.length() > 0) {
				newuser.setPassword(StringUtil.sha1(newpassword));
				boolean res = userService.updateUser(newuser);
				rs = new HashMap();
				if (res == true) {
					loginUser.setUser(newuser);
					rs.put("success", true);
					request.getSession().setAttribute("CURRENT_USER", loginUser);
				} else {
					rs.put("success", false);
				}
				rtn(rs, request, response);
				return;
			}
		}
		rs = new HashMap();
		rs.put("success", false);
		rtn(rs, request, response);
		return;
	}

	public void delete(HttpServletRequest request, HttpServletResponse response) {

		String idListStr = request.getParameter("idList");

		JSONArray jArray = JSONArray.fromObject(idListStr);

		Object[] idList = jArray.toArray();

		List userList = new ArrayList<User>();
		for (int i = 0; i < idList.length; i++) {
			User user = new User();
			user.setId((String) idList[i]);
			userList.add(user);
		}

		Map suc = userService.delete(userList);

		rtn(suc, request, response);

	}

	public void read(HttpServletRequest request, HttpServletResponse response) {

		String id = request.getParameter("id");
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		if (limit == null || limit.length() > 0) {
			limit = "10";
		}
		if (start == null || start.length() > 0) {
			start = "1";
		}
		Map condition = new HashMap();
		if (id != null) {
			condition.put("id", id);
		}
		condition.put("start", Integer.parseInt(start));
		condition.put("limit", Integer.parseInt(limit));
		Map map = userService.read(condition);
		// List translate_lst = this.tranToMapList(ls);
		List list = (List) map.get("list");
		Integer totalcount = (Integer) map.get("totalcount");
		Map rs = new HashMap();
		rs.put("items", list);
		rs.put("totalcount", totalcount);
		rtn(rs, request, response);

	}

	public void create(HttpServletRequest request, HttpServletResponse response) {

		User user = new User();
		Map rs = userService.create(request, response);

		rtn(rs, request, response);
	}

	public void readCbb(HttpServletRequest request, HttpServletResponse response) {

		Map cdt = new HashMap();
		cdt.put("departmentid", request.getParameter("departmentid"));

		Map map = userService.readCbb(cdt);
		rtn(map, request, response);

	}

	/** 将用户的信息转化为map列表 */
	private List tranToMapList(List ls) {
		List lsMap = new ArrayList();
		for (int i = 0; i < ls.size(); i++) {
			User user = (User) ls.get(i);
			Map userMap = new HashMap();
			userMap.put("id", user.getId());
			userMap.put("username", user.getUsername());
			userMap.put("password", user.getPassword());
			userMap.put("departmentid", user.getDepartmentid());
			lsMap.add(userMap);
		}

		return lsMap;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

}
